"""Extract text from html file"""
import argparse
import re
from html.parser import HTMLParser

from utils import split_enzh


class PageCollector(HTMLParser):
    """Collect paragraphs from html page"""
    def __init__(self):
        HTMLParser.__init__(self)
        self.doc = []
        self.p = []
        self.in_doc = False

    def handle_starttag(self, tag, attrs):
        # print('<%s>' % tag)
        if tag == "body":
            self.in_doc = True
        elif tag == "script" or tag == "style" or tag == "title"\
                or tag == "a":
            self.in_doc = False

        if tag == "br":
            self.doc.append("".join(self.p))
            self.p = []

    def handle_endtag(self, tag):
        # print('</%s>' % tag)
        if tag == "script" or tag == "style" or tag == "title"\
                or tag == "a":
            self.in_doc = True

        if tag == "p" or tag == "div" \
                or tag == "h3" or tag == "h2" or tag == "h1"\
                or tag == "ul" or tag == "li" or tag == "tr":
            self.doc.append("".join(self.p))
            self.p = []

        if tag == "span" and self.in_doc:
            self.p.append(" ")

    def handle_startendtag(self, tag, attrs):
        # print('<%s/>' % tag)
        if tag == "br":
            self.doc.append("".join(self.p))
            self.p = []

    def handle_data(self, data):
        if self.in_doc:
            self.p.append(data)

    # def handle_comment(self, data):
    #     print('<!--', data, '-->')
    #
    # def handle_entityref(self, name):
    #     print('&%s;' % name)
    #
    # def handle_charref(self, name):
    #     print('&#%s;' % name)


def get_text(html_fn):
    html_code = open(html_fn, encoding="utf-8").read()
    parser = PageCollector()
    parser.feed(html_code)
    parser.close()
    d = parser.doc

    # normalize various spaces
    paragraphes = [p.replace("\xa0", " ").replace("\n", " ").replace("\t", " ") for p in d]

    # remove the spaces at the begin and end
    paragraphes = [p.strip() for p in paragraphes]

    # condense multiple spaces
    paragraphes = [re.sub(r"\s{2,}", " ", p) for p in paragraphes]

    # delete blank lines
    paragraphes = list(filter(lambda p: len(p) > 0, paragraphes))

    return paragraphes


if __name__ == "__main__":
    argparser = argparse.ArgumentParser()
    argparser.add_argument("html_file")
    args = argparser.parse_args()

    p_list = get_text(args.html_file)
    print(p_list)

    en_p, zh_p = split_enzh(p_list)
    print(en_p)
    print(zh_p)
